#!/bin/bash
# test cgclear with one config file

. `dirname $0`/../testenv.sh

# start a process to torture
/bin/sleep 10000 &
PID=$!

# STEP1: parse one simple config with mount and cgclear it (incl. unmount)
$TOOLSDIR/cgconfigparser -l `prepare_config cgclear/cgclear_config_one.conf` \
	|| die "STEP1: cgconfigparser failed"
$TOOLSDIR/lscgroup > $TMP/groups.log
grep first <$TMP/groups.log >/dev/null \
	|| die "STEP1: cannot find group 'first'"

# unmount it
$TOOLSDIR/cgclear -l `prepare_config cgclear/cgclear_config_one.conf` \
	|| die "STEP1: cgclear failed"

# check all is gone
$TOOLSDIR/lscgroup 2>/dev/null \
	&& die "STEP1: something is mounted after cgclear"


# STEP2: create a cgroup out of config's tree - it must survive cgclear
$TOOLSDIR/cgconfigparser -l `prepare_config cgclear/cgclear_config_one.conf` \
	|| die "STEP2: cgconfigparser failed"
$TOOLSDIR/lscgroup > $TMP/groups.log
grep first <$TMP/groups.log >/dev/null \
	|| die "STEP2: cannot find group 'first'"

$TOOLSDIR/cgcreate -g cpu:/evil \
	|| die "STEP2: cgcreate failed"

# clear it - 'evil' should survive
$TOOLSDIR/cgclear -l `prepare_config cgclear/cgclear_config_one.conf` \
	|| die "STEP2: cgclear1 failed"

# check evil is there
$TOOLSDIR/lscgroup > $TMP/groups.log
grep evil <$TMP/groups.log >/dev/null \
	|| die "STEP2: evil not found"

# clear everything
$TOOLSDIR/cgclear || die "STEP2: cgclear2 failed"
$TOOLSDIR/lscgroup 2>/dev/null \
	&& die "STEP2: something is mounted after cgclear"


# STEP3: leave a process and a subgroup somewhere and check cgclear *without* -e clears it
$TOOLSDIR/cgconfigparser -l `prepare_config cgclear/cgclear_config_one.conf` \
	|| die "STEP3: cgconfigparser failed"
$TOOLSDIR/lscgroup > $TMP/groups.log
grep first <$TMP/groups.log >/dev/null \
	|| die "STEP3: cannot find group 'first'"

$TOOLSDIR/cgclassify -g cpu:first $PID \
	|| die "STEP3: cgclassify failed"
$TOOLSDIR/cgcreate -g cpu:first/evil \
	|| die "STEP3: cgcreate failed"

# unmount it
$TOOLSDIR/cgclear -l `prepare_config cgclear/cgclear_config_one.conf` \
	|| die "STEP3: cgclear failed"

# check all is gone
$TOOLSDIR/lscgroup 2>/dev/null \
	&& die "STEP3: something is mounted after cgclear"



# STEP4: leave a process somewhere and check cgclear with -e doesn't clear it
$TOOLSDIR/cgconfigparser -l `prepare_config cgclear/cgclear_config_one.conf` \
	|| die "STEP4: cgconfigparser failed"
$TOOLSDIR/lscgroup > $TMP/groups.log
grep first <$TMP/groups.log >/dev/null \
	|| die "STEP4: cannot find group 'first'"

$TOOLSDIR/cgclassify -g cpu:first $PID \
	|| die "STEP4: cgclassify failed"

# try to clear it - cgclear should not fail and the group should survive
$TOOLSDIR/cgclear -e -l `prepare_config cgclear/cgclear_config_one.conf` \
	|| die "STEP4: cgclear failed"
$TOOLSDIR/lscgroup > $TMP/groups2.log
grep first <$TMP/groups2.log >/dev/null \
	|| die "STEP4: cannot find group 'first' after cgclear"

# cpu:first should survive
$TOOLSDIR/lscgroup > $TMP/groups.log
grep first <$TMP/groups.log >/dev/null \
	|| die "STEP4: cannot find group 'first'"
# cpu:second should *not* survive
grep second <$TMP/groups.log >/dev/null \
	&& die "STEP4: unexpected group 'second' found"
# cpu:first/subgroup should *not* survive
grep first/subgroup  <$TMP/groups.log >/dev/null \
	&& die "STEP4: unexpected group 'first/subgroup' found"

$TOOLSDIR/cgclear

# check all is gone
$TOOLSDIR/lscgroup 2>/dev/null \
	&& die "STEP4: something is mounted after cgclear"



# STEP5: leave a subgroup somewhere and check cgclear with -e doesn't clear it
$TOOLSDIR/cgconfigparser -l `prepare_config cgclear/cgclear_config_one.conf` \
	|| die "STEP5: cgconfigparser failed"
$TOOLSDIR/lscgroup > $TMP/groups.log
grep first <$TMP/groups.log >/dev/null \
	|| die "STEP5: cannot find group 'first'"

$TOOLSDIR/cgcreate -g cpu:first/evil \
	|| die "STEP5: cgcreate failed"

# try to clear it - cgclear should not fail, the group should survive
$TOOLSDIR/cgclear -e -l `prepare_config cgclear/cgclear_config_one.conf` \
	|| die "STEP5: cgclear failed"
$TOOLSDIR/lscgroup > $TMP/groups2.log
grep first <$TMP/groups2.log >/dev/null \
	|| die "STEP5: cannot find group 'first' after clear"

# cpu:first/evil should survive
$TOOLSDIR/lscgroup > $TMP/groups.log
grep first/evil <$TMP/groups.log >/dev/null \
	|| die "STEP5: cannot find group 'first/evil'"
# cpu:second should *not* survive
grep second <$TMP/groups.log >/dev/null \
	&& die "STEP5: unexpected group 'second' found"
# cpu:first/subgroup should *not* survive
grep first/subgroup  <$TMP/groups.log >/dev/null \
	&& die "STEP5: unexpected group 'first/subgroup' found"

$TOOLSDIR/cgclear

# check all is gone
$TOOLSDIR/lscgroup 2>/dev/null \
	&& die "STEP4: something is mounted after cgclear"

kill -9 $PID

cleanup
exit 0
